package com.sbck.cs3.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sbck.cs3.MainActivity;
import com.sbck.cs3.MyGlobal;
import com.sbck.cs3.MyClass.MyOpenHelper;
import com.sbck.cs3.R;
import com.sbck.cs3.UNITINFO;

public class DeviceSelectFragment extends Fragment {
    MyOpenHelper myOpenHelper;
    SettingFragment settingFragment;
    RadioButton radioButton_Wifi;
    RadioButton radioButton_Net;

    AboutFragment aboutFragment;

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_deviceselect, container, false);

        settingFragment = new SettingFragment();

        aboutFragment = new AboutFragment();


        myOpenHelper = new MyOpenHelper(getActivity());
        myOpenHelper.GetAll();


        //读取系统配置
        myOpenHelper.Get_Setting();

        ListView listView = view.findViewById(R.id.lv_DeviceSelectList);
        listView.setAdapter(new MyAdapter());

        //网络模式按钮
        radioButton_Wifi = view.findViewById(R.id.rb_network_wifi);

        radioButton_Net = view.findViewById(R.id.rb_network_internet);


        //选择一个设备
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if(MyGlobal.deviceLogArrayList != null)
                    MyGlobal.deviceLogArrayList.clear();

                MyGlobal.setting.IsWifi.set(radioButton_Wifi.isChecked());

                MyGlobal.unitinfo = MyGlobal.unitinfoArrayList.get(i);
                MyGlobal.unitinfo.IsNew.set(false);

                Intent intent = new Intent();
                intent.setClass(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });

        //设置按钮
        Button buttonSet = view.findViewById(R.id.Btn_DeviceSelect_Set);
        buttonSet.setBackgroundColor(Color.GRAY);
        buttonSet.setTextColor(Color.WHITE);
        buttonSet.setOnClickListener(view13 -> getFragmentManager().beginTransaction().replace(R.id.fl_DeviceSelect_container,settingFragment).commitNowAllowingStateLoss());

        //添加新设备按钮
        Button buttonNew = view.findViewById(R.id.Btn_DeviceSelect_New);
        buttonNew.setOnClickListener(view12 -> {

            //网络模式按钮
            MyGlobal.setting.IsWifi.set(radioButton_Wifi.isChecked());

            MyGlobal.unitinfo = new UNITINFO();
            MyGlobal.unitinfo.IsNew.set(true);
            MyGlobal.unitinfo.DTU.set(MyGlobal.loginUser.LoginName.get());
            Intent intent = new Intent();
            intent.setClass(getActivity(),MainActivity.class);
            startActivity(intent);
        });

        //关于按钮
        Button buttonAbout = view.findViewById(R.id.Btn_DeviceSelect_About);
        buttonAbout.setOnClickListener(view14 -> getFragmentManager().beginTransaction().replace(R.id.fl_DeviceSelect_container,aboutFragment).commitNowAllowingStateLoss());

        return view;
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //网络模式按钮
        RadioButton radioButton_internet = view.findViewById(R.id.rb_network_internet);
        RadioButton radioButton_Wifi = view.findViewById(R.id.rb_network_wifi);

//        if((MyGlobal.loginUser.LoginName.get().equals("sbck") && MyGlobal.loginUser.Password.get().equals("sbck")))
//        {
//            if(MyGlobal.setting.IsWifi.get())
//                radioButton_Wifi.setChecked(true);
//            else
//                radioButton_internet.setChecked(true);
//        }
//        else
//        {
////            radioButton_Wifi.setChecked(true);
//            radioButton_Wifi.setVisibility(View.VISIBLE);
//            radioButton_internet.setVisibility(View.VISIBLE);
//            radioButton_Wifi.setEnabled(true);
//        }

        if(MyGlobal.setting.IsWifi.get())
            radioButton_Wifi.setChecked(true);
        else
            radioButton_internet.setChecked(true);
    }


    private class MyAdapter extends BaseAdapter {
        int count;

        public MyAdapter() {
            count = MyGlobal.unitinfoArrayList.size();
        }

        @Override
        public int getCount() {

            return  count;

        }

        @Override
        public Object getItem(int i) {
            return MyGlobal.unitinfoArrayList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            View v1;
            if (view == null) {
                v1 = View.inflate(getActivity(), R.layout.item_deviceselect, null);
            } else {
                v1 = view;
            }
            TextView tvKey = (TextView) v1.findViewById(R.id.item_deviceselect_dtu);
            TextView tvValue = (TextView) v1.findViewById(R.id.item_deviceselect_name);

            UNITINFO unitinfo = MyGlobal.unitinfoArrayList.get(i);
            tvKey.setText(unitinfo.DTU.get());
            tvValue.setText(unitinfo.Name.get());
            return v1;
        }
    }
}